package com.ag.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ag.bean.TbMedicineConfirm;


/**
 * 药品上报表的DAO
 * @author 李志伟
 *
 */

public class TbMedicineConfirmDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(TbMedicineConfirmDAO.class);
	// property constants
	public static final String MC_NAME = "mcName";
	public static final String MC_CNUM = "mcCnum";
	public static final String MC_MNUM = "mcMnum";
	public static final String MC_DNUM = "mcDnum";
	public static final String MC_WNUM = "mcWnum";
	public static final String A = "a";
	public static final String B = "b";
	public static final String C = "c";

	public void save(TbMedicineConfirm transientInstance) {
		log.debug("saving TbMedicineConfirm instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TbMedicineConfirm persistentInstance) {
		log.debug("deleting TbMedicineConfirm instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TbMedicineConfirm findById(java.lang.String id) {
		log.debug("getting TbMedicineConfirm instance with id: " + id);
		try {
			TbMedicineConfirm instance = (TbMedicineConfirm) getSession().get(
					"com.ag.bean.TbMedicineConfirm", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<TbMedicineConfirm> findByExample(TbMedicineConfirm instance) {
		log.debug("finding TbMedicineConfirm instance by example");
		try {
			List<TbMedicineConfirm> results = (List<TbMedicineConfirm>) getSession()
					.createCriteria("com.ag.bean.TbMedicineConfirm").add(
							create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TbMedicineConfirm instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TbMedicineConfirm as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<TbMedicineConfirm> findByMcName(Object mcName) {
		return findByProperty(MC_NAME, mcName);
	}

	public List<TbMedicineConfirm> findByMcCnum(Object mcCnum) {
		return findByProperty(MC_CNUM, mcCnum);
	}

	public List<TbMedicineConfirm> findByMcMnum(Object mcMnum) {
		return findByProperty(MC_MNUM, mcMnum);
	}

	public List<TbMedicineConfirm> findByMcDnum(Object mcDnum) {
		return findByProperty(MC_DNUM, mcDnum);
	}

	public List<TbMedicineConfirm> findByMcWnum(Object mcWnum) {
		return findByProperty(MC_WNUM, mcWnum);
	}

	public List<TbMedicineConfirm> findByA(Object a) {
		return findByProperty(A, a);
	}

	public List<TbMedicineConfirm> findByB(Object b) {
		return findByProperty(B, b);
	}

	public List<TbMedicineConfirm> findByC(Object c) {
		return findByProperty(C, c);
	}

	public List findAll() {
		log.debug("finding all TbMedicineConfirm instances");
		try {
			String queryString = "from TbMedicineConfirm";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TbMedicineConfirm merge(TbMedicineConfirm detachedInstance) {
		log.debug("merging TbMedicineConfirm instance");
		try {
			TbMedicineConfirm result = (TbMedicineConfirm) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TbMedicineConfirm instance) {
		log.debug("attaching dirty TbMedicineConfirm instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TbMedicineConfirm instance) {
		log.debug("attaching clean TbMedicineConfirm instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}